/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.serverpackets;

import l1j.server.server.model.L1Quest;
import l1j.server.server.model.Instance.L1PcInstance;

public class S_OtherCharPacks extends ServerBasePacket {

	private static final String S_OTHER_CHAR_PACKS = "[S] S_OtherCharPacks";
	private static final int STATUS_POISON = 1;
	private static final int STATUS_INVISIBLE = 2;
	private static final int STATUS_PC = 4;
	private static final int STATUS_BRAVE = 16;
	private static final int STATUS_ELFBRAVE = 32;
	private static final int STATUS_FASTMOVABLE = 64;
	private byte[] _byte = null;

	public S_OtherCharPacks(final L1PcInstance pc) {
		this.buildPacket(pc, false);
	}

	public S_OtherCharPacks(final L1PcInstance pc, final boolean isFindInvis) {
		this.buildPacket(pc, isFindInvis);
	}

	private void buildPacket(final L1PcInstance pc, final boolean isFindInvis) {
		int status = STATUS_PC;

		if (pc.getPoison() != null) { // 毒状態
			if (pc.getPoison().getEffectId() == 1) {
				status |= STATUS_POISON;
			}
		}
		if (pc.isInvisble() && !isFindInvis) {
			status |= STATUS_INVISIBLE;
		}
		if (pc.isBrave()) {
			status |= STATUS_BRAVE;
		}
		if (pc.isElfBrave()) {
			// エルヴンワッフルの場合は、STATUS_BRAVEとSTATUS_ELFBRAVEを立てる。
			// STATUS_ELFBRAVEのみでは効果が無い？
			status |= STATUS_BRAVE;
			status |= STATUS_ELFBRAVE;
		}
		if (pc.isFastMovable()) {
			status |= STATUS_FASTMOVABLE;
		}

		// int addbyte = 0;
		// int addbyte1 = 1;

		this.writeC(S_OPCODE_CHARPACK);
		this.writeH(pc.getX());
		this.writeH(pc.getY());
		this.writeD(pc.getId());
		if (pc.isDead()) {
			this.writeH(pc.getTempCharGfxAtDead());
			this.writeC(pc.getStatus());
		} else {
			this.writeH(pc.getTempCharGfx());
			this.writeC(pc.getCurrentWeapon());
		}
		this.writeC(pc.getHeading());
		// writeC(0); // makes char invis (0x01), cannot move. spells display
		this.writeC(pc.getChaLightSize());
		this.writeC(pc.getMoveSpeed());
		this.writeD(0x0000); // exp
		// writeC(0x00);
		this.writeH(pc.getLawful());
		StringBuilder title = new StringBuilder().append(pc.getName());
		// String title="";
		if (pc.isGm()) {
			title.append("\\f=【GM】");
		} else if (pc.isMonitor()) {
			title.append("\\f=【客服】");
		} else {
			final int LevelMet = pc.get_metempsychosis();
			if (LevelMet <= 30 && pc.getQuest().get_step(L1Quest.QUEST_opqlo新手保護) == 1) {
				title.append("\\fF【新手】");
			} else if(LevelMet >=200){
				title.append("\\fH【");
				title.append("狂皇子】");
			} else if (LevelMet >=150){
				title.append("\\fH【");
				if(LevelMet == 150){
				title.append("皇子】");
				}else{
					title.append("皇子").append(LevelMet).append("】");	
				}
			} else if (LevelMet >= 100) {
				title.append("\\fH【");
				if(LevelMet ==100){
				title.append("英雄】");
				}else{
					title.append("英雄").append(LevelMet).append("】");
				}
			} else {
				title.append("\\fH【");
				title.append(LevelMet);
				title.append("轉】");
			}
			if (pc.getClan()!=null) {
				final int rank = pc.getClanRank();
				// 2:一般 3:副君主 4:聯盟君主 5:修習騎士 6:守護騎士 7:一般 8:修習騎士 9:守護騎士 10:聯盟君主
				if(rank>2 && rank!=5 && rank!=7 && rank!=8){
					switch(rank){
					case 4: case 10:
						title.append("\\fA");
						break;
					case 3:
						title.append("\\fC");
						break;
					case 6: case 9:
						title.append("\\fB");
						break;
					}
				title.append(pc.getClan().getRankString(pc));
				}
			}
		}

		/*
		 * if(!pc.isGm()&&!pc.isMonitor()){ title.append(
		 * L1WilliamPlayerTitle.getTitle(pc, pc.getLevel())); }else
		 */
		this.writeS(title.toString());
		this.writeS(pc.getTitle());
		this.writeC(status);
		this.writeD(pc.getClanid());
		this.writeS(pc.getClanname()); // クラン名
		this.writeS(null); // ペッホチング？
		this.writeC(0); // ？
		/*
		 * if(pc.is_isInParty()) // パーティー中 { writeC(100 * pc.get_currentHp() /
		 * pc.get_maxHp()); } else { writeC(0xFF); }
		 */
		this.writeC(0xFF);
		
		if (pc.isSpeedUp()) {
			writeC(0x08); // 3段加速
		} else {
			writeC(0);
		}
//		this.writeC(0); // タルクック距離(通り)
		this.writeC(0); // PC = 0, Mon = Lv
		this.writeC(0); // ？
		this.writeC(0xFF);
		this.writeC(0xFF);
		
		title.delete(0, title.length());
		title = null;
	}

	@Override
	public byte[] getContent() {
		if (this._byte == null) {
			this._byte = this.getBytes();
		}
		return this._byte;
	}

	@Override
	public String getType() {
		return S_OTHER_CHAR_PACKS;
	}

}